//
//  NetWork.swift
//  MM2
//
//  Created by 李成明 on 2022/1/3.
//

import Foundation
import Moya
import HandyJSON

let provider = MoyaProvider<BaseApi>()

let requestBaseUrl = URL(string: "http://47.102.106.135:8080/")!
//let requestBaseUrl = URL(string: "http://127.0.0.1:8080/")!

let requestFaceDetectUrl = URL(string: "https://api-cn.faceplusplus.com/facepp/v3/detect")!

let requestFaceAnalyze = URL(string: "https://api-cn.faceplusplus.com/facepp/v3/face/analyze")!

let faceApiKey = "3jbj8L9xm6tdP-k2aVkjGWg1t5M530s_"
let faceApiSecret = "M_numVVwI5_BQLRp4arr0kSYFXiJlXYa"

let faceAnalyzeAttris = "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus"

enum BaseApi {
    case login(userName: String, password: String)
    case register(userName: String, password: String, avatar: String)
    case ossToken
    
    case uploadFaceResult(model: FaceUseResultModel, imageUrl: String, objectId: String)
    
    case faceDetect(api_key: String = faceApiKey, api_secret: String = faceApiSecret, image_base64: String, return_landmark: Int = 0)
    case faceDetectFile(api_key: String = faceApiKey, api_secret: String = faceApiSecret, image_file: Data)
    case faceDetectUrl(api_key: String = faceApiKey, api_secret: String = faceApiSecret, image_url: String)
    case faceAnalyze(api_key: String = faceApiKey, api_secret: String = faceApiSecret, face_tokens: String, return_attributes: String = faceAnalyzeAttris)
    
    case topRank
    case rankList(page: Int)
    case followAndFansNum(userId: Int)
    case postDaily(content: String, imgList: String)
    case dailyList(isFollow: Bool, page: Int)
    case followSomeone(toUserId: Int, isFollow: Bool)
    case commont(dailyId: Int, content: String, toReplyId: Int)
    case likeDaily(dailyId: Int, isLike: Bool)
    case perList(page: Int, userId: Int)
    case uploadOnlinPk(housenum: Int, model: FaceUseResultModel, imgUrl: String)
    case onlinePkResult(hoursenum: Int)
    case noticeList
}

extension BaseApi: TargetType {
    var baseURL: URL {
        switch self {
        case .faceDetect:
            return  requestFaceDetectUrl
        case .faceAnalyze:
            return requestFaceAnalyze
        case .faceDetectFile:
            return requestFaceDetectUrl
        case .faceDetectUrl:
            return requestFaceDetectUrl
        default:
            return requestBaseUrl
        }
    }
    
    var path: String {
        switch self {
        case .login:
            return "user/login"
        case .register:
            return "user/register"
        case .ossToken:
            return "oss/getOssToken"
        case .uploadFaceResult:
            return "value/estimate"
        case .faceAnalyze:
            return ""
        case .faceDetect:
            return ""
        case .faceDetectFile:
            return ""
        case .faceDetectUrl:
            return ""
        case .topRank:
            return "top/search"
        case .rankList:
            return "rank/rankPage"
        case .followAndFansNum:
            return "follow/followNum"
        case .postDaily:
            return "post/postNote"
        case .dailyList:
            return "note/noteList"
        case .followSomeone:
            return "isfollow/isfollow1"
        case .commont:
            return "post/comment"
        case .likeDaily:
            return "post/postLike"
        case .perList:
            return "per/perList"
        case .uploadOnlinPk:
            return "pk/timepk"
        case .onlinePkResult:
            return "getpk/pkinfo"
        case .noticeList:
            return "follow/notice"
        }
    }
    
    var method: Moya.Method {
        return .post
    }
    
    var task: Task {
        var params: [String: Any] = [:]
        params["userId"] = User.share.userId
        
        switch self {
        case .login(let userName, let password):
            params["username"] = userName
            params["password"] = password
        case .register(let userName, let password, let avatar):
            params["username"] = userName
            params["password"] = password
            params["avatar"] = avatar
            params["userId"] = nil
        case .ossToken:
            break
        case .faceDetect(let api_key, let api_secret, let image_base64, let return_landmark):
            params["api_key"] = api_key
            params["api_secret"] = api_secret
            params["image_base64"] = image_base64
            params["return_landmark"] = return_landmark
        case .faceAnalyze(let api_key, let api_secret, let face_tokens, let return_attributes):
            params["api_key"] = api_key
            params["api_secret"] = api_secret
            params["face_tokens"] = face_tokens
            params["return_attributes"] = return_attributes
        case .faceDetectFile(let api_key, let api_secret, let image_file):
            params["api_key"] = api_key
            params["api_secret"] = api_secret
            let muliData = MultipartFormData(provider: .data(image_file), name: "image_file")
            params["image_file"] = muliData
        case .faceDetectUrl(let api_key, let api_secret, let image_url):
            params["api_key"] = api_key
            params["api_secret"] = api_secret
            params["image_url"] = image_url
            
        case .uploadFaceResult(let model, let imgUrl, let objectId):
            params["faceValue"] = model.faceValue
            params["gender"] = model.gender
            params["age"] = model.age
            params["smile"] = model.smile
            params["emotion"] = model.emotion.desInt
            params["skinHealth"] = model.skinHealth
            params["skinStain"] = model.skinStain
            params["skinAcne"] = model.skinAcne
            params["skinDarkCircle"] = model.skinDarkCircle
            params["imgUrl"] = imgUrl
            params["objectId"] = objectId
        case .topRank:
            break
        case .rankList(let page):
            params["page"] = page
        case .followAndFansNum(let userId):
            params["userId"] = userId
        case .postDaily(let content, let imgList):
            params["content"] = content
            params["imgList"] = imgList
        case .dailyList(let isFollow, let page):
            params["follow"] = isFollow
            params["page"] = page
        case .followSomeone(let toUserId, let isFollow):
            params["isFollow"] = isFollow
            params["toUserId"] = toUserId
        case .commont(let dailyId, let content, let toReplyId):
            params["dailyId"] = dailyId
            params["content"] = content
            params["touserId"] = toReplyId
        case .likeDaily(let dailyId, let isLike):
            params["dailyId"] = dailyId
            params["like"] = isLike
        case .perList(let page, let userId):
            params["page"] = page
            params["userId"] = userId
        case .uploadOnlinPk(let housenum, let model, let imgUrl):
            params["faceValue"] = model.faceValue
            params["gender"] = model.gender
            params["age"] = model.age
            params["smile"] = model.smile
            params["emotion"] = model.emotion.desInt
            params["skinHealth"] = model.skinHealth
            params["skinStain"] = model.skinStain
            params["skinAcne"] = model.skinAcne
            params["skinDarkCircle"] = model.skinDarkCircle
            params["imgUrl"] = imgUrl
            params["hoursenum"] = housenum
        case .onlinePkResult(let hoursenum):
            params["hoursenum"] = hoursenum
        case .noticeList:
            break
        }
        return .requestParameters(parameters: params, encoding: URLEncoding.default)
    }
    
    var headers: [String : String]? {
        return nil
    }
}
